package com.briup.product_my.service.impl;

import com.briup.product_my.domain.DiseaseRecord;
import com.briup.product_my.domain.ManagerAnimal;
import com.briup.product_my.domain.ManagerDisease;
import com.briup.product_my.domain.ext.DiseaseRecordExt;
import com.briup.product_my.mapper.DiseaseRecordMapper;
import com.briup.product_my.mapper.ManagerAnimalMapper;
import com.briup.product_my.mapper.ManagerDiseaseMapper;
import com.briup.product_my.mapper.ext.DiseaseRecordExtMapper;
import com.briup.product_my.service.DiseaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/8/22-08-22-下午3:23
 * @Description: com.briup.product_my.service.impl
 */
@Service
public class DiseaseServiceImpl implements DiseaseService {
    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;
    @Autowired
    private DiseaseRecordExtMapper diseaseRecordExtMapper;
    @Autowired
    private ManagerDiseaseMapper managerDiseaseMapper;
    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;

    @Override
    public PageInfo<DiseaseRecordExt> findAll(Integer page, Integer pageSize, String drDid, String drStatus) {
        PageHelper.startPage(page, pageSize);
        List<DiseaseRecordExt> diseaseRecordext = diseaseRecordExtMapper.selectAll(drDid, drStatus);
        PageInfo<DiseaseRecordExt> pageInfo = new PageInfo<>(diseaseRecordext);
        return pageInfo;
    }

    @Override
    public List<ManagerDisease> queryAllDisease() {
        return managerDiseaseMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(DiseaseRecord diseaseRecord) {
        ManagerAnimal animal1 = managerAnimalMapper.selectById(diseaseRecord.getDrAnimalId());
        if(animal1.getAStatus().equals("养殖中")) {
            if (diseaseRecord.getDrId() == null) {
                //病症添加
                diseaseRecord.setDrId(managerDiseaseMapper.getMaxdrid() + 1);
                diseaseRecordMapper.insert(diseaseRecord);
                //动物状态更改
                ManagerAnimal animal = new ManagerAnimal();
                animal.setAAnimalId(diseaseRecord.getDrAnimalId());
                int count = 0;
                List<DiseaseRecord> diseaseRecords = diseaseRecordMapper.selectByAnimalId(diseaseRecord.getDrAnimalId());
                for (DiseaseRecord record : diseaseRecords) {
                    if (count == 0 && !record.getDrStatus().equals("已治疗")) {
                        count++;
                        animal.setAHealthy("生病");
                        managerAnimalMapper.updateByPrimaryKeySelective(animal);
                    }
                }

            } else {
                //病症修改
                diseaseRecordMapper.updateByPrimaryKeySelective(diseaseRecord);
                //动物状态更改
                ManagerAnimal animal = new ManagerAnimal();
                animal.setAAnimalId(diseaseRecord.getDrAnimalId());
                int count = 0;
                List<DiseaseRecord> diseaseRecords = diseaseRecordMapper.selectByAnimalId(diseaseRecord.getDrAnimalId());
                for (DiseaseRecord record : diseaseRecords) {
                    if (count == 0 && !record.getDrStatus().equals("已治疗")) {
                        count++;
                        animal.setAHealthy("生病");
                        managerAnimalMapper.updateByPrimaryKeySelective(animal);
                    }
                }
                if (count == 0) {
                    animal.setAHealthy("健康");
                    managerAnimalMapper.updateByPrimaryKeySelective(animal);
                }
            }
        }
    }

    @Override
    public Object indexCount() {
        Map<String,Integer> map = new HashMap<>();
        List<String> list = diseaseRecordExtMapper.selectName();
        for (String name : list) {
            map.put(name,diseaseRecordExtMapper.countByName(name));
        }
        return map;
    }

    @Override
    public void delete(String id) {
        diseaseRecordMapper.delete(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        diseaseRecordMapper.deleteAll(ids);
    }


}
